//
//  TimeAlertView.m
//  LightControl
//
//  Created by CKF on 2025/8/18.
//

#import "TimeAlertView.h"
#import "ClockView.h"
#import "Masonry.h"
#import "TapView.h"
@interface TimeAlertView()

@end
@implementation TimeAlertView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 注册键盘显示通知
           [[NSNotificationCenter defaultCenter] addObserver:self
                                                    selector:@selector(keyboardWillShow:)
                                                        name:UIKeyboardWillShowNotification
                                                      object:nil];
           
           // 注册键盘隐藏通知
           [[NSNotificationCenter defaultCenter] addObserver:self
                                                    selector:@selector(keyboardWillHide:)
                                                        name:UIKeyboardWillHideNotification
                                                      object:nil];
        
        __weak typeof(self)weakSelf = self;
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        _contentView  = [[UIView alloc]initWithFrame:CGRectMake(17, KScreenHeight*0.5-240, KScreenWidth-34, 582)];
        _contentView.backgroundColor = RGB(227, 228, 234);
        _contentView.layer.cornerRadius = 20;
        _contentView.layer.masksToBounds = YES;
        [self addSubview:_contentView];
        
        UILabel *timeLabel =  [[UILabel alloc]initWithFrame:CGRectMake(23, 23, KScreenWidth-80, 24)];
        [self.contentView addSubview:timeLabel];
        timeLabel.font = [UIFont systemFontOfSize:15];
        timeLabel.textColor = RGB(17, 17, 17);
        timeLabel.text = LocalString(@"选择时间");
        
        UIView *buttonView = [[UIView alloc]initWithFrame:CGRectMake(KScreenWidth-138, CGRectGetMaxY(timeLabel.frame)+20, 58, 91)];
        buttonView.backgroundColor = [UIColor whiteColor];
        buttonView.layer.cornerRadius = 5;
        buttonView.layer.masksToBounds = YES;
        buttonView.layer.borderColor = [UIColor blackColor].CGColor;
        buttonView.layer.borderWidth = 1;
        [self.contentView addSubview:buttonView];
        
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 45, 58, 1)];
        line.backgroundColor = [UIColor blackColor];
        [buttonView addSubview:line];
        
        _amButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _amButton.frame = CGRectMake(0, 0, 58, 45);
        [buttonView addSubview:_amButton];
        [_amButton setTitleColor:RGB(17, 17, 17) forState:0];
        [_amButton setTitle:LocalString(@"上午") forState:0];
        [_amButton setBackgroundColor:RGB(243, 213, 252)];
        [_amButton addTarget:self action:@selector(amButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        _pmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _pmButton.frame = CGRectMake(0, 46, 58, 45);
        [_pmButton setTitleColor:RGB(17, 17, 17) forState:0];
        [buttonView addSubview:_pmButton];
        [_pmButton setTitle:LocalString(@"下午") forState:0];
        [_pmButton addTarget:self action:@selector(pmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        _hourText = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(timeLabel.frame), CGRectGetMaxY(timeLabel.frame)+20, (KScreenWidth-158-40)*0.5, 91)];
        _hourText.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_hourText];
        _hourText.text = @"01";
        _hourText.font = [UIFont boldSystemFontOfSize:30];
        _hourText.userInteractionEnabled = false;
        _hourText.keyboardType = UIKeyboardTypeNumberPad;
        
        _hourTapView =  [[TapView alloc]initWithFrame:CGRectMake(CGRectGetMinX(timeLabel.frame), CGRectGetMaxY(timeLabel.frame)+20, (KScreenWidth-158-40)*0.5, 91)];
        _hourTapView.layer.cornerRadius = 5;
        _hourTapView.backgroundColor = RGB(200, 225, 254);
        _hourTapView.singleTapBlock = ^{
            if (weakSelf.clockView.selectType == SelectTypeMinute) {
                [weakSelf.clockView setSelectType:SelectTypeHour];
                weakSelf.hourTapView.backgroundColor = RGB(200, 225, 254);
            }
        };
        _hourTapView.doubleTapBlock  = ^{
            [weakSelf.hourText becomeFirstResponder];
            weakSelf.keyboardButton.selected = true;
            [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                weakSelf.contentView.frame = CGRectMake(17, KScreenHeight*0.5-240,  KScreenWidth-34, 582-288-40);
                weakSelf.clockView.transform = CGAffineTransformMakeScale(0.2, 0.2);
            } completion:^(BOOL finished) {
                weakSelf.clockView.hidden = YES;
            }];
        };
        [self.contentView addSubview:_hourTapView];
        
        UILabel *mhLabel =  [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_hourText.frame), CGRectGetMaxY(timeLabel.frame)+20, 40, 91)];
        [self.contentView addSubview:mhLabel];
        mhLabel.font = [UIFont systemFontOfSize:30];
        mhLabel.textColor = RGB(17, 17, 17);
        mhLabel.text = @":";
        mhLabel.textAlignment = NSTextAlignmentCenter;
        
        _miniteText = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(mhLabel.frame), CGRectGetMaxY(timeLabel.frame)+20, (KScreenWidth-158-40)*0.5, 91)];
        _miniteText.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_miniteText];
        _miniteText.text = @"01";
        _miniteText.userInteractionEnabled = false;
        _miniteText.font = [UIFont boldSystemFontOfSize:30];
        _miniteText.keyboardType = UIKeyboardTypeNumberPad;
        
        
        _minuteTapView =  [[TapView alloc]initWithFrame:CGRectMake(CGRectGetMinX(mhLabel.frame), CGRectGetMaxY(timeLabel.frame)+20, (KScreenWidth-158-40)*0.5, 91)];
        _minuteTapView.layer.cornerRadius = 5;
        _minuteTapView.backgroundColor =  RGB(220, 221, 228);
        _minuteTapView.singleTapBlock = ^{
            if (weakSelf.clockView.selectType == SelectTypeHour) {
                [weakSelf.clockView setSelectType:SelectTypeMinute];
                weakSelf.hourTapView.backgroundColor = RGB(200, 225, 254);
            }
        };
        _minuteTapView.doubleTapBlock = ^{
            [weakSelf.miniteText becomeFirstResponder];
            weakSelf.keyboardButton.selected = true;
            [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                weakSelf.contentView.frame = CGRectMake(17, KScreenHeight*0.5-240,  KScreenWidth-34, 582-288-40);
                weakSelf.clockView.transform = CGAffineTransformMakeScale(0.2, 0.2);
            } completion:^(BOOL finished) {
                weakSelf.clockView.hidden = YES;
            }];
        };
        [self.contentView addSubview:_hourTapView];
        
        _clockView = [[ClockView alloc]initWithFrame:CGRectMake(KScreenWidth*0.5-166, CGRectGetMaxY(buttonView.frame)+40, 286, 286)];
        [self.contentView addSubview:_clockView];
        
        _keyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_keyboardButton];
        [_keyboardButton setImage:[UIImage imageNamed:@""] forState:0];
        [_keyboardButton setImage:[UIImage imageNamed:@""] forState:UIControlStateSelected];
        [_keyboardButton addTarget:self action:@selector(keyboardButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_keyboardButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@23);
            make.height.equalTo(@63);
            make.width.equalTo(@60);
            make.bottom.equalTo(@-23);
        }];
        
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_sureButton];
        [_sureButton setTitle:LocalString(@"确定") forState:0];
        [_sureButton setTitleColor:RGB(42, 87, 128) forState:0];
        [_sureButton addTarget:self action:@selector(sureButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@-23);
            make.height.equalTo(@63);
            make.width.equalTo(@60);
            make.bottom.equalTo(@-23);
        }];
    
        _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:_cancelButton];
        [_cancelButton setTitle:LocalString(@"取消") forState:0];
        [_cancelButton setTitleColor:RGB(42, 87, 128) forState:0];
        [_cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.sureButton.mas_left).offset(-5);
            make.height.equalTo(@63);
            make.width.equalTo(@60);
            make.bottom.equalTo(@-22);
        }];
    }
    return self;
}

-(void)amButtonClick{}

-(void)pmButtonClick{}

-(void)sureButtonClick{}

-(void)cancelButtonClick{
    self.hidden = YES;
}

-(void)keyboardButtonClick{
    self.keyboardButton.selected =! self.keyboardButton.selected;
    if (self.keyboardButton.selected) {
        
    }
}


- (void)keyboardWillShow:(NSNotification *)notification {
    // 获取键盘的frame信息
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 获取键盘的高度
    CGFloat keyboardHeight = keyboardFrame.size.height;
    NSLog(@"Keyboard Height: %f", keyboardHeight);
    
    // 根据需要调整视图布局，例如调整某个输入框的位置或大小以适应键盘
}
 
- (void)keyboardWillHide:(NSNotification *)notification {
    // 键盘将要隐藏，可以做一些清理工作或者恢复之前的布局
}
@end
